package com.javafactroy.kunlong.bip.icp.context.connpool.testframe;

import com.javafactroy.kunlong.bip.icp.common.config.BipConfig;
import com.javafactroy.kunlong.bip.icp.common.consts.EAppType;
import com.javafactroy.kunlong.bip.icp.message.test.netty.NettyClientTMsg;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Scope("singleton")
@Data
public class NettyClientTMsgPoolFactory extends BasePooledObjectFactory<NettyClientTMsg> {
    @Autowired
    BipConfig bipConfig;

    public NettyClientTMsgPoolFactory() {

    }

    /**
     * 新建对象
     */
    @Override
    public NettyClientTMsg create() throws Exception {
        NettyClientTMsg nettyClient = new NettyClientTMsg();
        nettyClient.setBipConfig(bipConfig);
        nettyClient.start(EAppType.test);
        return nettyClient;
    }

    @Override
    public PooledObject<NettyClientTMsg> wrap(NettyClientTMsg client) {
        return new DefaultPooledObject<>(client);
    }

    /**
     * 销毁对象
     */
    @Override
    public void destroyObject(PooledObject<NettyClientTMsg> p) throws Exception {
        try {
            NettyClientTMsg client = p.getObject();
            client.close();
        } finally {
            super.destroyObject(p);
        }
    }

    /**
     * 验证对象
     */
    @Override
    public boolean validateObject(PooledObject<NettyClientTMsg> p) {
        return true;
    }


}

 
